﻿#include <wx/stdpaths.h>
//#include "wxSMTP/wxsmtp.h"
#include "FusionSpyActionDlg.h"

FusionSpyActionDlg::FusionSpyActionDlg( wxWindow* parent )
:
ActionDlg( parent )
{
	//dbWrapper.DBExist();
	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
	wxString DBName = StdDataDir + wxT("\\FusionSpy.db"); 

	dbWrapper.DBOpen( DBName, ADMINTABLE);
	if( dbWrapper.ActionSetQuery() > 0 ) {
		m_actionset = dbWrapper.getActionSet();
	}
}
FusionSpyActionDlg::~FusionSpyActionDlg()
{
	dbWrapper.DBClose();
}

void FusionSpyActionDlg::OnInitDlg( wxInitDialogEvent& event )
{
	m_txtSoundFile->WriteText( m_actionset.SoundFile );
	m_txtSMTPSvr->WriteText( m_actionset.SMTPSvr );
	m_txtSMTPUsr->WriteText( m_actionset.SMTPUsr );
	m_txtSMTPPwd->WriteText( m_actionset.SMTPPwd );
	m_txtMailTo->WriteText( m_actionset.MailTo );
	m_txtSMSTo->WriteText( m_actionset.SMSTo );
}

void FusionSpyActionDlg::OnOKButton( wxCommandEvent& event )
{
	m_actionset.SoundFile = m_txtSoundFile->GetValue();
	m_actionset.SMSTo = m_txtSMSTo->GetValue();
	m_actionset.SMTPPwd = m_txtSMTPPwd->GetValue();
	m_actionset.SMTPSvr = m_txtSMTPSvr->GetValue();
	m_actionset.SMTPUsr = m_txtSMTPUsr->GetValue();
	m_actionset.MailTo = m_txtMailTo->GetValue();

	dbWrapper.ActionSetUpdate( m_actionset );
/*
		wxSMTP *smtp = new wxSMTP(NULL);
		//smtp->SetHost(m_actionset.SMTPSvr);
		smtp->SetHost(wxT("smtp.sina.com"));
		wxEmailMessage *msg = new wxEmailMessage("Bug report",
												 "Your code really sucks.\n"
												 "Fix your code",
												 "fusionspy@sina.com.cn");
		msg->AddAlternative("<html><body><h1>Alarm report</h1>\n"
							"Your code <b>really</b> sucks <p>Fix your code</html>",
							"text","html");
		msg->AddTo( m_actionset.MailTo );
		smtp->Send(msg);
*/
	
	Destroy();
}

void FusionSpyActionDlg::OnBtnSoundFile(wxCommandEvent& event )
{
	wxString caption = wxT("Choose a sound file" );
	wxString wildcard = wxT("Wav files(*.wav)|*.wav");

	wxString defaultDir = wxStandardPaths::Get().GetDataDir();
	wxFileDialog dialog(this, caption, defaultDir, wxT(""),
		wildcard, wxOPEN );
	
	if( dialog.ShowModal() == wxID_OK )
	{
		wxString fileName = dialog.GetPath();
		m_txtSoundFile->WriteText( fileName );
	}
}